//
//  ASTextHeadTagView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/29.
//

#import "ASTextHeadTagView.h"
static NSString *ASTextHeadTagSubCellStr = @"ASTextHeadTagSubCell";
@interface ASTextHeadTagSubCell : UICollectionViewCell
@property (strong, nonatomic) UIView *mainView;
@property (strong, nonatomic) UILabel *titleLab;
@property (strong, nonatomic) UIView *borderView;
@end
@implementation ASTextHeadTagSubCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.mainView];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.borderView];
        [self setAllViewLayout];
    }
    return self;
}
- (void)setAllViewLayout{
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}
-(UIView *)mainView{
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.layer.masksToBounds = YES;
        _mainView.layer.cornerRadius = 16;
    }
    return _mainView;
}

-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = [UIFont systemFontOfSize:15];
        _titleLab.textColor = [UIColor colorWithHexString:@"1a1a1a"];
    }
    return _titleLab;
}

-(UIView *)borderView{
    if (!_borderView) {
        _borderView = [UIView new];
        _borderView.layer.borderColor = [UIColor colorWithHexString:@"#FC6783"].CGColor;
        _borderView.layer.borderWidth = 1;
        _borderView.backgroundColor = [UIColor clearColor];
        _borderView.layer.masksToBounds = YES;
        _borderView.layer.cornerRadius = 16;
    }
    return _borderView;
}

@end

@implementation ASTextHeadTagView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 10, 0));
        }];
    }
    return self;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fl.minimumLineSpacing = 10;
//        fl.minimumInteritemSpacing = 10;
        fl.sectionInset = UIEdgeInsetsMake(0, 13, 0, 13);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = self.backgroundColor;
        [_collectionView registerClass:[ASTextHeadTagSubCell class] forCellWithReuseIdentifier:ASTextHeadTagSubCellStr];
    }
    return _collectionView;
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        ASTextModel *model = self.dataArr[indexPath.row];
        CGFloat w = [ASTool getWidthWithText:model.name height:32 font:15];
        return CGSizeMake(w + 30, 32);
    }
    return CGSizeMake(100, 32);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASTextHeadTagSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASTextHeadTagSubCellStr forIndexPath:indexPath];
    if (self.dataArr.count > indexPath.row) {
        ASTextModel *model = self.dataArr[indexPath.row];
        cell.titleLab.text = model.name;
        cell.mainView.backgroundColor = [UIColor colorWithHexString:model.colorStr];
        if (model.ifSelect) {
            cell.borderView.hidden = NO;
        }else{
            cell.borderView.hidden = YES;
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (ASTextModel *model in self.dataArr) {
        model.ifSelect = NO;
    }
    if (self.dataArr.count > indexPath.row) {
        ASTextModel *model = self.dataArr[indexPath.row];
        model.ifSelect = YES;
    }
    [self.collectionView reloadData];
    if (self.clickSubIndexBlock) {
        self.clickSubIndexBlock(indexPath.row);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
